<?php

class Kd_Html_Form_Field extends Kd_Html_Element {

    private $_label = '';
    private $_input = NULL;
    private $_required = FALSE;
    private $_form = NULL;

    public function  __construct() {
        parent::__construct();
        $this->templateName = 'form/fieldset';
        $this->addClass('fieldset');
    }

    public function getLabel() {
        return $this->_label;
    }
    public function setLabel($value) {
        $this->_label = $value;
        return $this;
    }

    public function getInput() {
        return $this->_input;
    }
    public function setInput(Kd_Html_Form_Element $value) {
        $this->_input = $value->setField($this);
        return $this;
    }
    public function createInput($type, $id, $name = NULL) {
        $class = 'Kd_Html_Form_'.ucfirst($type);
        $result = new $class();
        $result->setId($id)->setName($name? $name: $id);
        $this->setInput($result);
        return $result;
    }

    public function getRequired() {
        return $this->_required;
    }
    public function setRequired($value) {
        $this->_required = $value;
        return $this;
    }

    public function getForm() {
        return $this->_form;
    }
    public function setForm($value) {
        $this->_form = $value;
        return $this;
    }

    protected function render() {
        if($this->getRequired()) {
            $this->addClass('required');
        }
        $result = '<tr'.$this->getAttributeHtml().'>';
        $result .= '<td class="label">';
        if(!$this->getInput()->getReadonly()) {
            $result .= '<label for="'.$this->getInput()->getId().'">'.$this->getLabel().'</label>';
        }else {
            $result .= $this->getLabel();
        }
        if($this->getRequired()) {
            $result .= '<span class="requried">*</span>';
        }
        $result .= '</td><td class="input">';
        $result .= $this->getInput()->toHtml();
        $result .= '</td></tr>';
        return $result;
    }
}